package com.wan.data.mysql.orm.meta;

import cn.hutool.core.util.StrUtil;
import com.wan.data.mysql.orm.convert.Converter;
import com.wan.util.cglib.FastClassUtil;
import com.wan.util.cglib.FastField;

import java.lang.reflect.Field;

public class ColumnMeta {
    /** 属性类型 */
    private final Class<?> fieldType;
    /** 属性快速访问器 */
    private final FastField fastField;
    /** 属性名称 */
    private final String fieldName;
    /** 字段名称 */
    private final String columnName;
    /** 长度 */
    private final long length;
    /** 小数点长度 */
    private final int scale;
    /** 不允许为空 */
    private final boolean notNull;
    /** 注释描述 */
    private final String comment;
    /** 转换器 */
    private final Class<? extends Converter> converter;
    /** 是否自增 */
    private final boolean autoIncrement;
    /** 是否主键 */
    private boolean pk = false;
    /** 字段类型：int(11) */
    private String columnType;

    public ColumnMeta(Field field,String columnName,
                      long length, int scale, boolean notNull,
                      String comment, Class<? extends Converter> converter,boolean auto) {
        this.fieldType = field.getType();
        this.fieldName = field.getName();
        this.fastField = FastClassUtil.getFastField(field);
        this.columnName = StrUtil.isEmpty(columnName) ? StrUtil.toUnderlineCase(fieldName) : columnName;
        this.length = length;
        this.scale = scale;
        this.notNull = notNull;
        this.comment = comment;
        this.converter = converter;
        this.autoIncrement = auto;
    }

    public ColumnMeta(String columnName,boolean notNull,String comment,String columnType,boolean auto){
        this.fieldType = null;
        this.fieldName = null;
        this.fastField = null;
        this.columnName = columnName;
        this.length = 0;
        this.scale = 0;
        this.notNull = notNull;
        this.comment = comment;
        this.converter = null;
        this.autoIncrement = auto;
        this.columnType = columnType;
    }

    public Class<?> getFieldType() {
        return fieldType;
    }

    public String getFieldName() {
        return fieldName;
    }

    public String getColumnName() {
        return columnName;
    }

    public long getLength() {
        return length;
    }

    public int getScale() {
        return scale;
    }

    public boolean isNotNull() {
        return notNull;
    }

    public String getComment() {
        return comment;
    }

    public Class<? extends Converter> getConverter() {
        return converter;
    }

    public FastField getFastField() {
        return fastField;
    }

    public void setPk() {
        this.pk = true;
    }

    public boolean isPk(){
        return pk;
    }

    public boolean isAutoIncrement() {
        return autoIncrement;
    }

    public String getColumnType() {
        return columnType;
    }

    public void setColumnType(String columnType) {
        this.columnType = columnType;
    }

    @Override
    public String toString() {
        String msg = "entity:[%s],field:[%s],fieldType:[%s]";
        return String.format(msg,fastField.getBeanClass().getCanonicalName(),fieldName,fieldType.getCanonicalName());
    }
}
